package com.whiteape.book.ui.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.whiteape.book.R;
import com.whiteape.book.core.manager.ReadSettingManager;
import com.whiteape.book.core.page.PageLoader;
import com.whiteape.book.ui.activity.BookReadActivity;
import com.whiteape.book.ui.base.BaseFragment;
import com.whiteape.book.widget.NumberPickerDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReadSettingPageStyleFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.rg_read_setting_para_spaces)
    RadioGroup mRGParaSpaces;

    @BindView(R.id.iv_para_interval_minus)
    ImageView mIvParaIntervalMinus;

    @BindView(R.id.iv_para_interval_add)
    ImageView mIvParaIntervalAdd;

    @BindView(R.id.tv_read_para_interval)
    TextView mTvParaInterval;

    @BindView(R.id.tv_read_line_interval)
    TextView mTvLineInterval;

    @BindView(R.id.iv_line_interval_minus)
    ImageView mIvLineIntervalMinus;

    @BindView(R.id.iv_line_interval_add)
    ImageView mIvLineIntervalAdd;

    @BindView(R.id.iv_page_left_minus)
    ImageView mIvLeftMinus;

    @BindView(R.id.iv_page_left_add)
    ImageView mIvLeftAdd;

    @BindView(R.id.iv_page_top_minus)
    ImageView mIvTopMinus;

    @BindView(R.id.iv_page_top_add)
    ImageView mIvTopAdd;

    @BindView(R.id.iv_page_right_minus)
    ImageView mIvRightMinus;

    @BindView(R.id.iv_page_right_add)
    ImageView mIvRightAdd;

    @BindView(R.id.iv_page_bottom_minus)
    ImageView mIvBottomMinus;

    @BindView(R.id.iv_page_bottom_add)
    ImageView mIvBottomAdd;

    @BindView(R.id.tv_read_top_margin)
    TextView mTvTopMargin;

    @BindView(R.id.tv_read_left_margin)
    TextView mTvLeftMargin;

    @BindView(R.id.tv_read_right_margin)
    TextView mTvRightMargin;

    @BindView(R.id.tv_read_bottom_margin)
    TextView mTvBottomMargin;

    @BindView(R.id.iv_page_tip_left_minus)
    ImageView mIvTipLeftMinus;

    @BindView(R.id.iv_page_tip_left_add)
    ImageView mIvTipLeftAdd;

    @BindView(R.id.iv_page_tip_top_minus)
    ImageView mIvTipTopMinus;

    @BindView(R.id.iv_page_tip_top_add)
    ImageView mIvTipTopAdd;

    @BindView(R.id.iv_page_tip_right_minus)
    ImageView mIvTipRightMinus;

    @BindView(R.id.iv_page_tip_right_add)
    ImageView mIvTipRightAdd;

    @BindView(R.id.iv_page_tip_bottom_minus)
    ImageView mIvTipBottomMinus;

    @BindView(R.id.iv_page_tip_bottom_add)
    ImageView mIvTipBottomAdd;

    @BindView(R.id.tv_read_tip_top_margin)
    TextView mTvTipTopMargin;

    @BindView(R.id.tv_read_tip_left_margin)
    TextView mTvTipLeftMargin;

    @BindView(R.id.tv_read_tip_right_margin)
    TextView mTvTipRightMargin;

    @BindView(R.id.tv_read_tip_bottom_margin)
    TextView mTvTipBottomMargin;

    private int mParaStartSpacesNum;
//    private int mCurMarginLeft;
//    private int mCurMarginTop;
//    private int mCurMarginRight;
//    private int mCurMarginBottom;
//    private int mCurTipMarginLeft;
//    private int mCurTipMarginTop;
//    private int mCurTipMarginRight;
//    private int mCurTipMarginBottom;
//    //段落间距
//    private int mCurParaInterval;
//    //行间距
//    private int mCurLineInterval;

    private List<Integer> mCurNumList = new ArrayList<>();

    private final int MAX_MARGIN_LEFT = 80;
    private final int MAX_MARGIN_RIGHT = 80;
    private final int MAX_MARGIN_TOP = 80;
    private final int MAX_MARGIN_BOTTOM = 80;
    private final int MAX_PARA_INTERVAL = 50;
    private final int MAX_LINE_INTERVAL = 50;
    private final int MAX_TIP_MARGIN_LEFT = 300;
    private final int MAX_TIP_MARGIN_RIGHT = 80;
    private final int MAX_TIP_MARGIN_TOP = 80;
    private final int MAX_TIP_MARGIN_BOTTOM = 80;

    private final List<Integer> minusIds = Arrays.asList(R.id.iv_para_interval_minus, R.id.iv_line_interval_minus,
            R.id.iv_page_left_minus, R.id.iv_page_top_minus, R.id.iv_page_right_minus, R.id.iv_page_bottom_minus,
            R.id.iv_page_tip_left_minus, R.id.iv_page_tip_top_minus, R.id.iv_page_tip_right_minus, R.id.iv_page_tip_bottom_minus);
    private final List<Integer> addIds = Arrays.asList(R.id.iv_para_interval_add, R.id.iv_line_interval_add,
            R.id.iv_page_left_add, R.id.iv_page_top_add, R.id.iv_page_right_add, R.id.iv_page_bottom_add,
            R.id.iv_page_tip_left_add, R.id.iv_page_tip_top_add, R.id.iv_page_tip_right_add, R.id.iv_page_tip_bottom_add);

    private final List<Integer> tvIds = Arrays.asList(
            R.id.tv_read_para_interval, R.id.tv_read_line_interval,
            R.id.tv_read_left_margin, R.id.tv_read_top_margin, R.id.tv_read_right_margin, R.id.tv_read_bottom_margin,
            R.id.tv_read_tip_left_margin, R.id.tv_read_tip_top_margin, R.id.tv_read_tip_right_margin, R.id.tv_read_tip_bottom_margin);

    private List<TextView> textViewList;

    private final List<Integer> maxMargins = Arrays.asList(MAX_PARA_INTERVAL, MAX_LINE_INTERVAL,
            MAX_MARGIN_LEFT, MAX_MARGIN_TOP, MAX_MARGIN_RIGHT, MAX_MARGIN_BOTTOM,
            MAX_TIP_MARGIN_LEFT, MAX_TIP_MARGIN_TOP, MAX_TIP_MARGIN_RIGHT, MAX_TIP_MARGIN_BOTTOM);

    private final List<Integer> settingIds = Arrays.asList(ReadSettingManager.READ_MARGIN_TYPE_PARA_INTERVAL, ReadSettingManager.READ_MARGIN_TYPE_LINE_INTERVAL,
            ReadSettingManager.READ_MARGIN_TYPE_LEFT, ReadSettingManager.READ_MARGIN_TYPE_TOP, ReadSettingManager.READ_MARGIN_TYPE_RIGHT, ReadSettingManager.READ_MARGIN_TYPE_BOTTOM,
            ReadSettingManager.READ_MARGIN_TYPE_TIP_LEFT, ReadSettingManager.READ_MARGIN_TYPE_TIP_TOP, ReadSettingManager.READ_MARGIN_TYPE_TIP_RIGHT, ReadSettingManager.READ_MARGIN_TYPE_TIP_BOTTOM);

    private PageLoader mPageLoader;
    private Activity mActivity;

    public ReadSettingPageStyleFragment(/*Activity activity, PageLoader pageLoader*/) {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.mActivity = getActivity();
        this.mPageLoader = ((BookReadActivity) mActivity).getPageLoader();
        initRadioGroup();

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mIvParaIntervalMinus.setOnClickListener(this);
        mIvParaIntervalAdd.setOnClickListener(this);
        mIvLineIntervalMinus.setOnClickListener(this);
        mIvLineIntervalAdd.setOnClickListener(this);
        mIvBottomAdd.setOnClickListener(this);
        mIvBottomMinus.setOnClickListener(this);
        mIvLeftMinus.setOnClickListener(this);
        mIvLeftAdd.setOnClickListener(this);
        mIvTopMinus.setOnClickListener(this);
        mIvTopAdd.setOnClickListener(this);
        mIvRightMinus.setOnClickListener(this);
        mIvRightAdd.setOnClickListener(this);
        mIvTipBottomAdd.setOnClickListener(this);
        mIvTipBottomMinus.setOnClickListener(this);
        mIvTipLeftMinus.setOnClickListener(this);
        mIvTipLeftAdd.setOnClickListener(this);
        mIvTipTopMinus.setOnClickListener(this);
        mIvTipTopAdd.setOnClickListener(this);
        mIvTipRightMinus.setOnClickListener(this);
        mIvTipRightAdd.setOnClickListener(this);

        mTvLeftMargin.setOnClickListener(this);
        mTvTopMargin.setOnClickListener(this);
        mTvRightMargin.setOnClickListener(this);
        mTvBottomMargin.setOnClickListener(this);
        mTvTipLeftMargin.setOnClickListener(this);
        mTvTipTopMargin.setOnClickListener(this);
        mTvTipRightMargin.setOnClickListener(this);
        mTvTipBottomMargin.setOnClickListener(this);
        mTvParaInterval.setOnClickListener(this);
        mTvLineInterval.setOnClickListener(this);

        mRGParaSpaces.setOnCheckedChangeListener((group, checkedId) -> {
            if (mActivity == null) {
                return;
            }
            for (int i = 0; i < mRGParaSpaces.getChildCount(); i++) {
                RadioButton radioButton = (RadioButton) mRGParaSpaces.getChildAt(i);
                if (radioButton.getId() == checkedId) {
                    if (mParaStartSpacesNum != i + 1) {
                        //只有点击的不是当前的 才有反应
                        mParaStartSpacesNum = i + 1;
                        ReadSettingManager.getInstance().setReadParaStartSpacesNum(mParaStartSpacesNum);
                        updateMargin();
                    }
                    radioButton.setTextColor(ContextCompat.getColor(mActivity, R.color.orange));
                } else {
                    radioButton.setTextColor(ContextCompat.getColor(mActivity, R.color.read_menu_text));
                }
            }
        });
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        textViewList = Arrays.asList(mTvParaInterval, mTvLineInterval,
                mTvLeftMargin, mTvTopMargin, mTvRightMargin, mTvBottomMargin,
                mTvTipLeftMargin, mTvTipTopMargin, mTvTipRightMargin, mTvTipBottomMargin);


//        for (int i = 0; i < tvIds.size(); i++) {
//            textViewList.get(i).setText(mCurNumList.get(i));
//        }
        mTvParaInterval.setText("" + mCurNumList.get(0));
        mTvLineInterval.setText("" + mCurNumList.get(1));
        mTvLeftMargin.setText("" + mCurNumList.get(2));
        mTvTopMargin.setText("" + mCurNumList.get(3));
        mTvRightMargin.setText("" + mCurNumList.get(4));
        mTvBottomMargin.setText("" + mCurNumList.get(5));
        mTvTipLeftMargin.setText("" + mCurNumList.get(6));
        mTvTipTopMargin.setText("" + mCurNumList.get(7));
        mTvTipRightMargin.setText("" + mCurNumList.get(8));
        mTvTipBottomMargin.setText("" + mCurNumList.get(9));

    }

    private void initRadioGroup() {
        for (int i = 0; i < mRGParaSpaces.getChildCount(); i++) {
            RadioButton radioButton = (RadioButton) mRGParaSpaces.getChildAt(i);
            if (i + 1 == mParaStartSpacesNum) {
                radioButton.performClick();
                break;
//                radioButton.setSelected(true);
//                radioButton.setTextColor(ContextCompat.getColor(mActivity, R.color.orange));
            } else {
//                radioButton.setSelected(false);
//                radioButton.setTextColor(ContextCompat.getColor(mActivity, R.color.read_menu_text));
            }
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mParaStartSpacesNum = ReadSettingManager.getInstance().getReadParaStartSpacesNum();

        if (mCurNumList.size() == 0) {
            for (int i = 0; i < tvIds.size(); i++) {
                mCurNumList.add(ReadSettingManager.getInstance().getReadMargin(settingIds.get(i)));
            }
        }
//        mCurMarginLeft = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_LEFT);
//        mCurMarginTop = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_TOP);
//        mCurMarginRight = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_RIGHT);
//        mCurMarginBottom = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_BOTTOM);
//        mCurTipMarginLeft = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_TIP_LEFT);
//        mCurTipMarginTop = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_TIP_TOP);
//        mCurTipMarginRight = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_TIP_RIGHT);
//        mCurTipMarginBottom = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_TIP_BOTTOM);
//        mCurParaInterval = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_PARA_INTERVAL);
//        mCurLineInterval = ReadSettingManager.getInstance().getReadMargin(ReadSettingManager.READ_MARGIN_TYPE_LINE_INTERVAL);
    }

    @Override
    protected int getContentId() {
        return R.layout.dialog_read_setting_page_style;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (minusIds.contains(id)) {
            int index = minusIds.indexOf(id);
            if (mCurNumList.get(index) > 0) {
                mCurNumList.set(index, mCurNumList.get(index) - 1);
            }
            updateMargin();
        } else if (addIds.contains(id)) {
            int index = addIds.indexOf(id);
            if (mCurNumList.get(index) < maxMargins.get(index)) {
                mCurNumList.set(index, mCurNumList.get(index) + 1);
            }
            updateMargin();
        } else if (tvIds.contains(id)) {
            int index = tvIds.indexOf(id);
            new NumberPickerDialog(getActivity(), value -> {
                mCurNumList.set(index, value);
                updateMargin();
            }, maxMargins.get(index), 0, mCurNumList.get(index)).show();


        }

    }

    private void save() {
        for (int i = 0; i < tvIds.size(); i++) {
            ReadSettingManager.getInstance().setReadMargin(settingIds.get(i), mCurNumList.get(i));
            textViewList.get(i).setText("" + mCurNumList.get(i));

        }
    }

    private void updateMargin() {
        save();
        if (mPageLoader != null) {
//            mPageLoader.updateMargin(mParaStartSpacesNum, mCurParaInterval, mCurLineInterval, mCurMarginLeft, mCurMarginTop, mCurMarginRight, mCurMarginBottom);
            mPageLoader.updateMargin();
        }

    }
}
